﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Devices.Sensors;


namespace LongOnEveryday
{
    public partial class MainPage : PhoneApplicationPage, INotifyPropertyChanged
    {
        private Accelerometer _accelerometer;

        private double _x;
        public double X
        {
            get { return _x; }
            set { _x = value; OnPropertyChanged("X"); }
        }

        private double _y;
        public double Y
        {
            get { return _y; }
            set { _y = value; OnPropertyChanged("Y"); }
        }

        private double _z;
        public double Z
        {
            get { return _z; }
            set { _z = value; OnPropertyChanged("Z"); }
        }

        // Constructor
        public MainPage()
        {
            InitializeComponent();

            this.DataContext = this;

            _accelerometer = new Accelerometer();
            _accelerometer.ReadingChanged += acc_ReadingChanged;
            _accelerometer.Start();

        }

        void acc_ReadingChanged(object sender, Microsoft.Devices.Sensors.AccelerometerReadingEventArgs e)
        {
            Dispatcher.BeginInvoke(() =>
                                       {

                                           X =  e.X;
                                           Y =  e.Y;
                                           Z =  e.Z;
                                       });


        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}